import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';

import CommonConstants from '../common/constants/CommonConstants';
import {
  columnUserInfoList,
  columnOrderInfoList,
  columnMessageInfoList,
  goodsOrderInfoList,
  goodsInfoList,
  cardManagerInfoList,
  shoppingCartInfoBeanList
} from '../viewModel/RdbColumnModel';

import RdbUtils from '../common/database/rdb/RdbUtils';
import Logger from '../common/utils/Logger';
import window from '@ohos.window';


export default class EntryAbility extends UIAbility {
  async onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');

    RdbUtils.initDb(this.context, CommonConstants.RDB_NAME.dbName ? CommonConstants.RDB_NAME.dbName : '');
    await RdbUtils.createDb();

    RdbUtils.createTable(CommonConstants.USER_INFO.tableName ? CommonConstants.USER_INFO.tableName : '', columnUserInfoList).then(() => {
      Logger.info(`RdbHelper createTable taskInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper taskInfo err : ${JSON.stringify(err)}`);
    });

    RdbUtils.createTable(CommonConstants.CARD_MANAGER_INFO.tableName ? CommonConstants.CARD_MANAGER_INFO.tableName : '', cardManagerInfoList).then(() => {
      Logger.info(`RdbHelper createTable dayInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper dayInfo err : ${JSON.stringify(err)}`);
    });

    RdbUtils.createTable(CommonConstants.GOODS_INFO.tableName ? CommonConstants.GOODS_INFO.tableName : '', goodsInfoList).then(() => {
      Logger.info(`RdbHelper createTable globalInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper globalInfo err : ${JSON.stringify(err)}`);
    });
    RdbUtils.createTable(CommonConstants.GOODS_ORDER_INFO.tableName ? CommonConstants.GOODS_ORDER_INFO.tableName : '', goodsOrderInfoList).then(() => {
      Logger.info(`RdbHelper createTable taskInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper taskInfo err : ${JSON.stringify(err)}`);
    });

    RdbUtils.createTable(CommonConstants.MESSAGE_INFO.tableName ? CommonConstants.MESSAGE_INFO.tableName : '', columnMessageInfoList).then(() => {
      Logger.info(`RdbHelper createTable taskInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper taskInfo err : ${JSON.stringify(err)}`);
    });


    RdbUtils.createTable(CommonConstants.ORDER_INFO.tableName ? CommonConstants.ORDER_INFO.tableName : '', columnOrderInfoList).then(() => {
      Logger.info(`RdbHelper createTable taskInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper taskInfo err : ${JSON.stringify(err)}`);
    });

    RdbUtils.createTable(CommonConstants.SHOPPING_CART_INFO_BEAN.tableName ? CommonConstants.SHOPPING_CART_INFO_BEAN.tableName : '',shoppingCartInfoBeanList ).then(() => {
      Logger.info(`RdbHelper createTable taskInfo success`);
    }).catch((err: Error) => {
      Logger.error(`RdbHelper taskInfo err : ${JSON.stringify(err)}`);
    });

  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
